package com.rsc.learningbar;

import com.rsc.learningbar.dao.StudentPlanRepository;
import com.rsc.learningbar.entity.StudentPlan;
import com.rsc.learningbar.service.PlanNotifyService;
import com.rsc.learningbar.vo.Plan;
import com.rsc.learningbar.vo.StudentPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import javax.annotation.PostConstruct;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.TimeUnit;

/**
 * 我的第一个SpringBoot程序
 * 其中 @RestController 等同于 （@Controller 与 @ResponseBody）
 *
 * @author Levin
 */
@RestController
@SpringBootApplication
@EnableWebSocket
public class LearningbarApplication {

    @Autowired
    StudentPlanRepository studentPlanRepository;

    public static void main(String[] args) {
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
        ConfigurableApplicationContext context = SpringApplication.run(LearningbarApplication.class, args);
        context.getBean(PlanNotifyService.class).start();
    }

    @Autowired
    DelayQueue delayQueue;

    /**
     * 启动加载
     */
    @PostConstruct
    public void postMethod() {
        // 发送过期的学习提醒
        List<StudentPlan> plans = studentPlanRepository.findTodoPlan();
        for (StudentPlan plan : plans) {
            // 放入延迟队列
            StudentPlanVo info = new StudentPlanVo();
            info.setId(plan.getId());
            info.setUserId(plan.getUserId());
            info.setStudentMissionId(plan.getStudentMissionId());
            info.setStudentMissionName(plan.getStudentMissionName());

            LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime dateTime = LocalDateTime.parse(plan.getRemindTime(), formatter);

            Duration duration = Duration.between(localDateTime, dateTime);

            long seconds = duration.getSeconds();
            Plan p = new Plan(info, seconds, TimeUnit.SECONDS);
            if (!delayQueue.contains(p)) {
                delayQueue.put(p);
            }
        }
    }

    @Bean
    public DelayQueue<Plan> delayQueue() {
        return new DelayQueue<>();
    }

    /**
     * 如果直接使用springboot的内置容器，而不是使用独立的servlet容器，就要注入ServerEndpointExporter，外部容器则不需要。
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}
